package edu.bw.web.dept;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.pojo.Dept;
import edu.bw.service.DeptService;
import edu.bw.service.impl.DeptServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/dept/insert")
public class InsertDeptServlet extends HttpServlet {
    DeptService deptService =new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{id: 0, deptName: "测试01", tel: "17858925679", email: "3201631766@qq.com", desc: null}
        String reqJSON = request.getReader().readLine();
        Dept dept = JSONUtil.toBean(reqJSON, Dept.class);

        //调service层处理
        int rows=deptService.insert(dept);
        System.out.println(CommonResult.ok().put("rows", rows));

        //向前端响应结果
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
